package mysql

import (
	"gorm.io/gorm"
	"video-guard/videorpc/videorpc"
)

type CommentInfo struct {
	gorm.Model
	VideoId        int    `gorm:"type:int(11)"`
	UserId         int    `gorm:"type:int(11)"`
	CommentContent string `gorm:"varchar(100)"`
	IsAnonymous    int    `gorm:"type:int(11)"`
	CommentStatus  int    `gorm:"type:int(11)"`
	ReplyId        int    `gorm:"type:int(11)"`
}

func (i *CommentInfo) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *CommentInfo) FirstCommentById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(i).Error
}

func (i *CommentInfo) CommentDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(i).Error
}

func CommentList(db *gorm.DB, page int64, size int64) ([]*videorpc.CommentList, error) {

	var commentList []*videorpc.CommentList

	err2 := db.Debug().Model(&CommentInfo{}).Select(
		"user_infos.nick_name",
		"comment_infos.comment_content",
		"comment_infos.created_at",
	).Joins("LEFT JOIN user_infos ON user_infos.id=comment_infos.user_id").
		Scopes(Paginate(int(page), int(size))).
		Order("comment_infos.created_at DESC").
		Find(&commentList).Error

	return commentList, err2
}
